package com.myk.game.shapechesstool.views;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.GradientDrawable;

public class BorderDrawable extends GradientDrawable {

    private final int borderColor;
    private final int[] borderWidths;
    private final int[] borderRadius;

    public BorderDrawable(int borderColor, int[] borderWidths, int[] borderRadius) {
        this.borderColor = borderColor;
        this.borderWidths = borderWidths;
        this.borderRadius = borderRadius;
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        drawBorder(canvas, borderColor, borderWidths, borderRadius);
    }

    /**
     * 绘制border
     */
    private void drawBorder(Canvas canvas, final int borderColor, final int[] borderWidths, final int[] borderRadius) {
        //获取当前canvas的宽高
        Rect rect = canvas.getClipBounds();
        final int width = rect.width();
        final int height = rect.height();

        int borderWidthLeft;
        int borderWidthTop;
        int borderWidthRight;
        int borderWidthBottom;

        //取得我们的边框宽度,并附加给相应变量
        if (borderWidths != null && borderWidths.length == 4) {
            borderWidthLeft = Math.min(width / 2, borderWidths[0]);
            borderWidthTop = Math.min(height / 2, borderWidths[1]);
            borderWidthRight = Math.min(width / 2, borderWidths[2]);
            borderWidthBottom = Math.min(height / 2, borderWidths[3]);
        }
        else {
            return;
        }

        // 设置画笔
        Paint paint = new Paint();
        //抗锯齿
        paint.setAntiAlias(true);
        //画笔颜色
        paint.setColor(borderColor);
        //画笔样式
        paint.setStyle(Paint.Style.STROKE);
        //设置边框宽度
        paint.setStrokeWidth(borderWidthLeft);

        // 判断当前边框是否相等
        if ((borderWidthLeft == borderWidthTop) && (borderWidthLeft == borderWidthRight) && (borderWidthLeft == borderWidthBottom)) {
            if (borderWidthLeft == 0) {
                return;
            }
            // borderRadius != null且borderWidth!-0;计算并画出圆角边框，否则为直角边框
            if (borderRadius != null && borderRadius.length == 4) {
                int sum = 0;
                /**
                 * 循环传递的最后一个参数,相加
                 * 是数组的原因是适应更多的边框需求,因为你不一定四个边框都是一个圆角度数
                 */
                for (int radius : borderRadius) {
                    if (radius < 0) {
                        return;
                    }
                    sum += radius;
                }
                //如果传递的都是0直接绘制即可
                if (sum == 0) {
                    canvas.drawRect(rect, paint);
                }
                int borderWidth = borderWidthLeft;
                float halfBorderWidth = borderWidth / 2f;

                int mMaxRadiusX = (int) (width / 2f - halfBorderWidth);
                int mMaxRadiusY = (int) (height / 2f - halfBorderWidth);

                int topLeftRadiusX = Math.min(mMaxRadiusX, borderRadius[0]);
                int topLeftRadiusY = Math.min(mMaxRadiusY, borderRadius[0]);
                int topRightRadiusX = Math.min(mMaxRadiusX, borderRadius[1]);
                int topRightRadiusY = Math.min(mMaxRadiusY, borderRadius[1]);
                int bottomRightRadiusX = Math.min(mMaxRadiusX, borderRadius[2]);
                int bottomRightRadiusY = Math.min(mMaxRadiusY, borderRadius[2]);
                int bottomLeftRadiusX = Math.min(mMaxRadiusX, borderRadius[3]);
                int bottomLeftRadiusY = Math.min(mMaxRadiusY, borderRadius[3]);

                //绘制左上圆角,通过旋转来达到圆角的效果,本质上其实绘制的是圆弧
                if (topLeftRadiusX < borderWidth || topLeftRadiusY < borderWidth) {
                    RectF arc1 = new RectF(0, 0, topLeftRadiusX * 2, topLeftRadiusY * 2);
                    paint.setStyle(Paint.Style.FILL);
                    canvas.drawArc(arc1, 180, 90, true, paint);
                }
                else {
                    RectF arc1 = new RectF(halfBorderWidth, halfBorderWidth, topLeftRadiusX * 2 - halfBorderWidth, topLeftRadiusY * 2 - halfBorderWidth);
                    paint.setStyle(Paint.Style.STROKE);
                    canvas.drawArc(arc1, 180, 90, false, paint);
                }
                //绘制上方的边框
                canvas.drawLine(topLeftRadiusX, halfBorderWidth, width - topRightRadiusX, halfBorderWidth, paint);

                //绘制右上圆角
                if (topRightRadiusX < borderWidth || topRightRadiusY < borderWidth) {
                    RectF arc2 = new RectF(width - topRightRadiusX * 2, 0, width, topRightRadiusY * 2);
                    paint.setStyle(Paint.Style.FILL);
                    canvas.drawArc(arc2, 270, 90, true, paint);
                }
                else {
                    RectF arc2 = new RectF(width - topRightRadiusX * 2 + halfBorderWidth, halfBorderWidth, width - halfBorderWidth, topRightRadiusY * 2 - halfBorderWidth);
                    paint.setStyle(Paint.Style.STROKE);
                    canvas.drawArc(arc2, 270, 90, false, paint);
                }
                //绘制右边边框
                canvas.drawLine(width - halfBorderWidth, topRightRadiusY, width - halfBorderWidth, height - bottomRightRadiusY, paint);
                //绘制右下圆角
                if (bottomRightRadiusX < borderWidth || bottomRightRadiusY < borderWidth) {
                    RectF arc3 = new RectF(width - bottomRightRadiusX * 2, height - bottomRightRadiusY * 2, width, height);
                    paint.setStyle(Paint.Style.FILL);
                    canvas.drawArc(arc3, 0, 90, true, paint);
                }
                else {
                    RectF arc3 = new RectF(width - bottomRightRadiusX * 2 + halfBorderWidth, height - bottomRightRadiusY * 2 + halfBorderWidth, width - halfBorderWidth, height - halfBorderWidth);
                    paint.setStyle(Paint.Style.STROKE);
                    canvas.drawArc(arc3, 0, 90, false, paint);
                }
                //绘制底部边框
                canvas.drawLine(bottomLeftRadiusX, height - halfBorderWidth, width - bottomRightRadiusX, height - halfBorderWidth, paint);
                //绘制左下圆角
                if (bottomLeftRadiusX < borderWidth || bottomLeftRadiusY < borderWidth) {
                    RectF arc4 = new RectF(0, height - bottomLeftRadiusY * 2, bottomLeftRadiusX * 2, height);
                    paint.setStyle(Paint.Style.FILL);
                    canvas.drawArc(arc4, 90, 90, true, paint);
                }
                else {
                    RectF arc4 = new RectF(halfBorderWidth, height - bottomLeftRadiusY * 2 + halfBorderWidth, bottomLeftRadiusX * 2 - halfBorderWidth, height - halfBorderWidth);
                    paint.setStyle(Paint.Style.STROKE);
                    canvas.drawArc(arc4, 90, 90, false, paint);
                }
                //绘制左边边框
                canvas.drawLine(halfBorderWidth, topLeftRadiusY, halfBorderWidth, height - bottomLeftRadiusY, paint);
            }
            else {
                //如果没有传递圆角的参数,直接绘制即可
                canvas.drawRect(rect, paint);
            }
        }
        else {
            //当边框的宽度不同时,绘制不同的线粗,通过borderWidthLeft,rect.top,rect.bottom来确定每根线所在的位置
            if (borderWidthLeft > 0) {
                paint.setStrokeWidth(borderWidthLeft);
                canvas.drawLine(borderWidthLeft / 2f, rect.top, borderWidthLeft / 2f, rect.bottom, paint);
            }
            if (borderWidthTop > 0) {
                paint.setStrokeWidth(borderWidthTop);
                canvas.drawLine(rect.left, borderWidthTop / 2f, rect.right, borderWidthTop / 2f, paint);
            }
            if (borderWidthRight > 0) {
                paint.setStrokeWidth(borderWidthRight);
                canvas.drawLine(rect.right - borderWidthRight / 2f, rect.top, rect.right - borderWidthRight / 2f, rect.bottom, paint);
            }
            if (borderWidthBottom > 0) {
                paint.setStrokeWidth(borderWidthBottom);
                canvas.drawLine(rect.left, rect.bottom - borderWidthBottom / 2f, width, rect.bottom - borderWidthBottom / 2f, paint);
            }
        }
    }
}
